[!if FPCPRT != ""]
	Copyright (c) 2011-2012 by MicroStrategy, Inc.  This copyrighted material
	is the confidential, unpublished property of MicroStrategy.  This
	copyright notice and any other copyright notices included in machine
	readable copies must be reproduced on all authorized copies.
[!endif]
//=======================================================================
[!output COPYRIGHT_MSG]
//
//		File:	RSupport.h
//
//		Date:	[!output FPDATE]
//
//		Desc:	Definition of CRSupport class.
//=======================================================================

#ifndef __RSUPPORT_H_
#define __RSUPPORT_H_
#include <windows.h>
#include <R.h>
#include <Rinternals.h>
#include <Rembedded.h>

// R type definitions
typedef int (*p_TYPEOF)(SEXP x);
typedef int (*pRf_initEmbeddedR)(int argc, char *argv[]);
typedef int (*pR_finite)(double);
typedef int *(*p_INTEGER)(SEXP x);
typedef int *(*p_LOGICAL)(SEXP x);
typedef void (*pRf_unprotect)(int);
typedef void (*pRf_PrintValue)(SEXP);
typedef	void (*pRf_endEmbeddedR)(int fatal);
typedef void (*pRf_defineVar)(SEXP, SEXP, SEXP);
typedef void (*p_SET_STRING_ELT)(SEXP, int, SEXP);
typedef const char * (*pRf_translateChar)(SEXP);
typedef double *(*p_REAL)(SEXP x);
typedef double (*pRf_asReal)(SEXP x);
typedef R_len_t (*pRf_length)(SEXP);
typedef SEXP (*pRf_protect)(SEXP);
typedef SEXP (*pRf_eval)(SEXP, SEXP);
typedef SEXP (*pRf_lang2)(SEXP, SEXP);
typedef SEXP (*pRf_ScalarInteger)(int);
typedef SEXP (*pRf_findVar)(SEXP, SEXP);
typedef SEXP (*pRf_install)(const char *);
typedef SEXP (*pRf_mkChar)(const char *);
typedef SEXP (*pRf_mkString)(const char *);
typedef SEXP (*pR_tryEval)(SEXP, SEXP, int *);
typedef SEXP (*pRf_allocVector)(SEXPTYPE, R_len_t);
typedef SEXP (*pRf_allocMatrix)(SEXPTYPE, int, int);
typedef SEXP (*p_STRING_ELT)(SEXP x, int i);

class CRSupport
{
private:
	CComBSTR			m_sRInstallPath;		// root of R installation
	HINSTANCE			m_hinstLib;				// handle for R.dll
	int					m_initR;				// flag indicating state of R initialization (0 = not initialized or failed initialization)

	pRf_initEmbeddedR	m_Rf_initEmbeddedR_win;
	pRf_protect			m_Rf_protect_win;
	pRf_lang2			m_lang2_win;
	pRf_install			m_install_win;
	pRf_mkChar			m_mkChar_win;
	pRf_mkString		m_mkString_win;
	pR_tryEval			m_R_tryEval_win;
	pRf_unprotect		m_Rf_unprotect_win;
	pRf_PrintValue		m_Rf_PrintValue_win;
	pRf_endEmbeddedR	m_Rf_endEmbeddedR_win; 
	pRf_allocVector		m_Rf_allocVector_win;
	pRf_allocMatrix		m_Rf_allocMatrix_win;
	p_REAL				m_REAL_win;
	p_INTEGER			m_INTEGER_win;
	p_LOGICAL			m_LOGICAL_win;
	pRf_defineVar		m_defineVar_win;
	pRf_asReal			m_asReal_win;
	pRf_ScalarInteger	m_ScalarInteger_win;
	pRf_length			m_length_win;
	p_TYPEOF			m_TYPEOF_win;
	pRf_findVar			m_findVar_win;
	pRf_eval			m_eval_win;
	SEXP				*m_R_GlobalEnv_win;
	SEXP				*m_R_UnboundValue_win;
	p_SET_STRING_ELT	m_SET_STRING_ELT_win;
	p_STRING_ELT		m_STRING_ELT_win;
	pRf_translateChar	m_translateChar_win;
	pR_finite			m_R_finite_win;
	double				*m_pNaReal_win;
	int					*m_pNaInteger_win;
	int					*m_pNaLogical_win;
	SEXP				*m_pNaString_win;

	Int_32 InitR();
	void EndR();
	STDMETHOD(GetRInstallPath)();
public:
	bool InitOK() { return (m_initR != 0); }
	CRSupport(void);
	virtual ~CRSupport(void);
	void SetRVar(const char *pName, Int_32 nSize, double *pData, DSSData_Flags *pFlag);
	void SetRVar_rp(const char *pName, Int_32 nSize, Int_32 lPIndex, Int_32 lRepCnt, double *pData, DSSData_Flags *pFlag);
	STDMETHOD(SetRVar_mx)(const char *pName, Int_32 lPIndex, Int_32 nRows, Int_32 nCols, double *pData, DSSData_Flags *pFlag);
	STDMETHOD(SetRVarV)(const char *pName, VARIANT *pValue, DSSData_Flags *pFlag);
	STDMETHOD(SetRVarSA)(const char *pName, Int_32 nSize, EnumDSSDataType dataType, VARIANT *pData, DSSData_Flags *pFlag);
	STDMETHOD(SetRVarSA_rp)(const char *pName, Int_32 nSize, Int_32 lPIndex, Int_32 lInCnt, Int_32 lRepCnt,
		EnumDSSDataType dataType, VARIANT *pData, DSSData_Flags *pFlag);
	STDMETHOD(SetRVarSA_mx)(const char *pName, Int_32 lPIndex, Int_32 nRows, Int_32 nCols, EnumDSSDataType dataType,
		VARIANT *pData, DSSData_Flags *pFlag);
	Int_32 SubmitR(const char *script);
	void GetRVar(const char *pName, Int_32 nSize, Int_32 *pRSize, double *pResult, DSSData_Flags *pFlag);
[!if FUNCPACKAGE_NONNUMERIC]
	STDMETHOD(GetRVarNN)(const char *pName, Int_32 nStrLen, Int_32 nSize, Int_32 *pRSize, VARIANT *pResult, DSSData_Flags *pFlag);
[!endif]
};
#endif // __RSUPPORT_H_



